package deepbrain.simnetwork.mechanism;

import deepbrain.simnetwork.network.Network;
import deepbrain.simnetwork.network.NetworkState;
import deepbrain.simnetwork.network.NodeState;
import deepbrain.simnetwork.structure.Connection;

/**
 * Hopfield Mechanism with temperature
 * 
 * @author Li Yang
 * @create 2009-3-28
 */
public class PLoSHopfieldMechanism extends GenericMechanism {

	protected double temperature = 0.35;

	public PLoSHopfieldMechanism(double temperature) {
		this.temperature = temperature;
	}
	
	public NodeState nextNodeState(int node, Network network, NetworkState state) {
		double power = computeSigma(node, network, state) / temperature;
		double prob = Math.exp(power) / (Math.exp(power) + Math.exp(-power));
		return NodeState.newNodeState(prob);
	}

	@SuppressWarnings("unchecked")
	private double computeSigma(int node, Network network, NetworkState state) {
		double sum = 0;
		for (Connection<?> conn : network.getAfferentConnections(node)) {
			Connection<Double> connection = (Connection<Double>) conn;
			sum += connection.getWeight()
					* state.getNodeState(connection.source)
							.getFiringProbability();
		}
		return sum;
	}

	@Override
	public String toString() {
		return "Hopfield Mechanism in PLoS paper";
	}

}
